#include "dataframe.h"
#include "biosignal.h"

#include <iostream>

using namespace std;

namespace biosignal {
using namespace std::tr1;

DataFrame::DataFrame(const BioSignal* biosignal, TimePeriod bounds,
                     shared_ptr<const montage::Montage> montage) :
        m_biosignal(biosignal) {
    if (montage) {
        m_montage = montage;
    } else {
        m_montage = m_biosignal->ref_montage();
    }
}

DataFrame::~DataFrame() {
    m_signal_data.clear();
}


shared_ptr<const montage::Montage> DataFrame::montage() const {
    return m_montage;
}

void DataFrame::add_signaldata(SignalData* signal_data) {
    shared_ptr<SignalData> sdp = shared_ptr<SignalData> (signal_data);
    m_signal_data.push_back(sdp);
    m_signal_data_map[signal_data->signal_info()->label()] = sdp;
}

TimePeriod DataFrame::time_period() const {
    return m_time_period;
}

void DataFrame::time_period(TimePeriod time_period) {
    m_time_period = time_period;
}

shared_ptr<const SignalData> DataFrame::signal_source_data(
    std::string signal_source_label) const {
    shared_ptr<SignalData> sdp =
        m_signal_data_map.find(signal_source_label)->second;
    shared_ptr<const SignalData> result = const_pointer_cast<const SignalData>(sdp);
    return result;
}

DataFrame::const_iterator DataFrame::begin() {
    return channels_data().begin();
}

DataFrame::const_iterator DataFrame::end() {
    return channels_data().end();
}

const std::vector<shared_ptr<SignalData> >& DataFrame::signal_source_data() const {
    return m_signal_data;
}

const std::vector<montage::ChannelDataType>& DataFrame::channels_data() {
    if (m_channels_data.empty()) {
        m_channels_data = m_montage->create_channels_data( this );
    }
    return m_channels_data;
}

}
